<?php

//用户可删除已用余额支付的订单，并将余额退回帐户
define("PAYMENT_NOT_EXIST",0); //支付单被删除(提示联系管理员)
define("PAY_SUCCESS",1);  //充值成功(充值到相应的会员帐户中，并生成日志)或者订单支付成功
define("ORDER_REPAY",2);    //订单重复支付(即付款单所属的订单已支付，将支付的金额转存到会员帐户，并生成日志)
define("ORDER_EXPIRED",3);   //订单支付失败(限时已到，无法完成订单支付，退款到会员帐户，并生成日志)
define("ORDER_SOLDOUT",4);   //订单支付失败(即库存已满，无法完成订单支付，退款到会员帐户，并生成日志)

//付款记录支付
//返回
function payment_paid($notice_sn,$outer_sn) {
    $payment_notice = M('payment_notice')->where(array('notice_sn' => $notice_sn))->find();
    if ($payment_notice) {
        $res = M('payment_notice')->where(array('id' => $payment_notice['id'],'is_paid' => '0'))->data(array('pay_time' => get_gmtime(),'is_paid' => '1','outer_notice_sn' => $outer_sn))->save();
        if ($res > 0) {//如果订单未支付
            M('payment')->where(array('id' => $payment_notice['payment_id']))->data(array('total_amount' => array('exp',"total_amount+{$payment_notice['money']}")))->save();

            $order_id = $payment_notice['order_id'];
            $order_info = M('deal_order')->where(array('id' => $order_id))->find();
            if ($order_info) {
                if ($order_info['order_status'] > 0) {
                    //已支付
                    $payment_info = M('payment')->where(array('id' => $payment_notice['payment_id']))->find();
                    require_once SITE_PATH . "system/libs/user.php";
                    require_once SITE_PATH . "system/payment/" . $payment_info['class_name'] . "_payment.php";
                    $log_info = "通过" . $payment_info['name'] . $payment_notice['bank_id'] . "支付，单号" . $outer_sn . "，订单已支付过";
                    //modify_account(array("money" => $payment_notice['money']), $payment_notice['user_id'], $log_info);
                    return array("info" => $log_info,"jump" => url("user#view_order",array('id'=>$order_id)));
                } else {
                    //开始支付订单
                    M('deal_order')->where(array('id' => $order_info['id']))->data(array('online_pay' => $payment_notice['money']))->save();
                    $result = pay_order($order_info['id']);
                    if ($result['status'] == 1) {
                        return array("info" => "已过期","jump" => url("user#view_order",array('id'=>$order_id)));
                    }
                    if ($result['status'] == 2) {
                        return array("info" => "限额已满","jump" => url("user#view_order",array('id'=>$order_id)));
                    }
                    if ($result['status'] == 3) {
                        send_order_success($order_id,$order_info['user_name'],$order_info['user_id'],$order_info['user_name'],$order_info['deal_name']);
                        return array("info" => "支付成功","jump" => url("user#view_order",array('id'=>$order_id)));
                    }
                    if ($result['status'] == 0) {
                        return array("info" => "订单被篡改，支付失败","jump" => url("user#view_order",array('id'=>$order_id)));
                    }
                }
            } else {
                $payment_info = M('payment')->where(array('id' => $payment_notice['payment_id']))->find();
                require_once SITE_PATH . "system/libs/user.php";
                $payment_lang = array();
                require_once SITE_PATH . "system/payment/" . $payment_info['class_name'] . "_payment.php";
                $log_info = "通过" . $payment_info['name'] . $payment_lang[$payment_notice['bank_id']] . "支付，单号" . $outer_sn . "，支付的订单已失效";
                modify_account(array("money" => $payment_notice['money']),$payment_notice['user_id'],$log_info);
                return array("info" => $log_info,"jump" => url("user#support"));
            }

        } else //付款单已支付
        {
            $order_id = $payment_notice['order_id'];
            $order_info = M('deal_order')->where(array('id' => $order_id))->find();
            if ($order_info) {
                if ($order_info['order_status'] > 0) {
                    //已支付
                    if ($order_info['order_status'] == 1) {
                        return array("info" => "已过期","jump" => url("user#view_order",array('id'=>$order_id)));
                    }
                    if ($order_info['order_status'] == 2) {
                        return array("info" => "限额已满","jump" => url("user#view_order",array('id'=>$order_id)));
                    }
                    if ($order_info['order_status'] == 3) {
                        return array("info" => "支付成功","jump" => url("user#view_order",array('id'=>$order_id)));
                    }
                    if ($order_info['order_status'] == 0) {
                        return array("info" => "订单被篡改，支付失败","jump" => url("user#view_order",array('id'=>$order_id)));
                    }
                } else {
                    //开始支付订单
                    M('deal_order')->where(array('id' => $order_info['id']))->data(array('online_pay' => $payment_notice['money']))->save();
                    $result = pay_order($order_info['id']);
                    if ($result['status'] == 1) {
                        return array("info" => "已过期","jump" => url("user#view_order",array('id'=>$order_id)));
                    }
                    if ($result['status'] == 2) {
                        return array("info" => "限额已满","jump" => url("user#view_order",array('id'=>$order_id)));
                    }
                    if ($result['status'] == 3) {
                        return array("info" => "支付成功","jump" => url("user#view_order",array('id'=>$order_id)));
                    }
                    if ($result['status'] == 0) {
                        return array("info" => "订单被篡改，支付失败","jump" => url("user#view_order",array('id'=>$order_id)));
                    }
                }
            } else {
                $payment_info = M('payment')->where(array('id' => $payment_notice['payment_id']))->find();
                require_once SITE_PATH . "system/payment/" . $payment_info['class_name'] . "_payment.php";
                $log_info = "通过" . $payment_info['name'] . $payment_notice['bank_id'] . "支付，单号" . $outer_sn . "，支付的订单已失效";
                return array("info" => $log_info,"jump" => url("user#view_order",array('id'=>$order_id)));
            }
        }
    } else {
        return array("info" => "无效的支付单号，请联系管理员","jump" => url("index"));
    }
}


?>